package com.abc.http.example.high.token;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import com.abc.http.example.common.model.HttpResult;
import com.yansl.http.intercepter.HttpIntercepter;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;

/**
 * @description: 返回值统一处理器
 * @author: yansl.cn
 * @create: 2023-01-17 11:34
 */
@Component
public class TokenHttpIntercepter implements HttpIntercepter {

    @Override
    public Class getHttpIntercepterClass() {
        return TokenHttpIntercepter.class;
    }

    /**
     * 请求数据统一处理转换
     * 
     * @param responseEntity
     *            请求结果
     * @return
     */
    @Override
    public String requestAfter(ResponseEntity<String> responseEntity) {
        if (responseEntity.getStatusCodeValue() == HttpStatus.OK.value()) {
            HttpResult<String> httpResult = JSONUtil.toBean(responseEntity.getBody(), new TypeReference<HttpResult<String>>() {
            }, true);
            return httpResult.getRs();
        }
        return null;
    }
}
